<?php
namespace App\Http\Controllers\Admin\Product;

use App\Http\Controllers\Admin\BaseController;
use Illuminate\Http\Request;

class AttributeController extends BaseController
{
    protected  $api_url = [
        'index' => ['get', 'product/attr'],
        'show' => ['get', 'product/attr/%d'],
        'store' => ['post', 'product/attr'],
        'update' => ['put', 'product/attr/%d'],
        'delete' => ['delete', 'product/attr/%d'],
        
        'attr_val_index' => ['get', 'product/attr_value'],
    ];
    
    protected $api_host;
    
    protected $request;
    
    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->api_host = config('url.api_backend');
    }
    
    public function indexAction(){
        $this->validate($this->request, [
            'page' => 'integer',
            'name' => 'string',
        ]);
        $search_params = $this->request->all(['name']);
        $page = $this->request->get('page', 1);
        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'orderBy'=>'id' ,
            'sortedBy'=>'desc',
            'page' => $page
        ];
        
        $ret = http_admin($this->api_url['index'])->send($params);
        $data = [];
        if(isset($ret['code']) && $ret['code']===0 && !empty($ret['data'])){
            $data = $ret['data'];
        }
        
        $data['search_params'] = $search_params;
         
        return view('Admin.product.attribute', paginate($this->request, $data));
    }
    
    public function ajaxValuesAction($id){
        $this->validate($this->request, [
            'page' => 'integer',
            'perPage' => 'integer',
        ]);
        $search_params = ['attribute_id'=>$id];
        $page = $this->request->get('page', 1);
        $perPage = $this->request->get('perPage', 100);
        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'orderBy'=>'id' ,
            'sortedBy'=>'desc',
            'page' => $page,
            'perPage'=>$perPage,
        ];
        
        
        $ret = http_admin($this->api_url['attr_val_index'])->send($params);
        $data = [];
        if(isset($ret['code']) && $ret['code']===0 && !empty($ret['data'])){
            $data = $ret['data'];
        }
        
        return response()->json(['code'=>0,'data'=>$data]);
    }
    
    public function showAction($id){
        $ret = http_admin($this->api_url['show'],[$id])->send();
        $data = [];
        if(isset($ret['code']) && $ret['code']===0 && !empty($ret['data'])){
            $data = $ret['data'];
        }
        return view('Admin.product.attribute_detail', $data);
    }
    
    public function storeAction(){
        $this->validate($this->request, [
            'name' => 'string',
        ]);
        $data = $this->request->all([]);
   
        $ret = http_admin($this->api_url['store'])->send($data);
        
        if(isset($ret['code']) && $ret['code']===0 && !empty($ret['data'])){
            return response()->json(['code'=>0,'message'=>'']);
        }else{
            return response()->json(['code'=>1,'message'=>isset($ret['message']) ? $ret['message'] : $ret['data']]);
        }
       
    }
    
    public function updateAction($id){
        $this->validate($this->request, [
            'name' => 'required'
        ]);
        $data = $this->request->all([]);
        
        $ret = http_admin($this->api_url['update'],[$id])->send($data);
     
        if(isset($ret['code']) && $ret['code']===0 ){
            return response()->json(['code'=>0,'message'=>'']);
        }else{
            return response()->json(['code'=>1,'message'=>isset($ret['message']) ? $ret['message'] : $ret['data']]);
        }
    }
    
    public function ajaxUpdateAction($id){
        $data = $this->request->all([]);
        $ret = http_admin($this->api_url['update'],[$id])->send($data);
        
        if(isset($ret['code']) && $ret['code']===0 ){
            return response()->json(['code'=>0,'message'=>'']);
        }else{
            return response()->json(['code'=>1,'message'=>isset($ret['message']) ? $ret['message'] : $ret['data']]);
        }
    }
    
    
    public function deleteAction($id){
        $ret = http_admin($this->api_url['delete'],[$id])->send();
        if(isset($ret['code']) && $ret['code']===0){
            return response()->json(['code'=>0,'message'=>$ret['message']]);
        }else{
            return response()->json(['code'=>1,'message'=>$ret['message']]);
        }
    }
    
}